package com.sinba.common.user.action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.sinba.common.dto.PageDTO;
import com.sinba.common.user.dto.AccountDTO;
import com.sinba.common.user.dto.RegisterUserDTO;
import com.sinba.common.user.service.IAccountService;
import com.sinba.common.user.service.IRegisterUserService;

@SuppressWarnings("serial")
public class AccountAction extends ActionSupport {
	@Autowired
	private IAccountService accountService;
	@Autowired
	private IRegisterUserService registerUserService;
	private String type;
	private String loginName;
	private Double value;
	private String transAccount;
	private String status;//操作状态
	private AccountDTO accountDTO = new AccountDTO();
	private RegisterUserDTO registerUserDTO = new RegisterUserDTO();
	private Double extractMoney;//提取金额
	private PageDTO pageDTO = new PageDTO();
	private String upgradeLevel;
	private String pageUrl;
	private String resultStr;
	
	public RegisterUserDTO getRegisterUserDTO() {
		return registerUserDTO;
	}

	public void setRegisterUserDTO(RegisterUserDTO registerUserDTO) {
		this.registerUserDTO = registerUserDTO;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public Double getValue() {
		return value;
	}

	public void setValue(Double value) {
		this.value = value;
	}

	
	public String getTransAccount() {
		return transAccount;
	}

	public void setTransAccount(String transAccount) {
		this.transAccount = transAccount;
	}
	
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public AccountDTO getAccountDTO() {
		return accountDTO;
	}

	public void setAccountDTO(AccountDTO accountDTO) {
		this.accountDTO = accountDTO;
	}

	public Double getExtractMoney() {
		return extractMoney;
	}

	public void setExtractMoney(Double extractMoney) {
		this.extractMoney = extractMoney;
	}
	public PageDTO getPageDTO() {
		return pageDTO;
	}

	public void setPageDTO(PageDTO pageDTO) {
		this.pageDTO = pageDTO;
	}

	public String getUpgradeLevel() {
		return upgradeLevel;
	}

	public void setUpgradeLevel(String upgradeLevel) {
		this.upgradeLevel = upgradeLevel;
	}

	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public String getResultStr() {
		return resultStr;
	}

	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}

	public String deltaTransfer(){
		if(pageUrl==null || pageUrl=="")
			pageUrl="index.jsp";
		HttpSession session = ServletActionContext.getRequest().getSession();
		if(session.getAttribute("loginName")!=null && registerUserDTO != null){//获取登录会话
			String loginName = (String)session.getAttribute("loginName");
			registerUserDTO.setLoginName(loginName);
			if(!registerUserService.checkPwdProblem(registerUserDTO)){//验证密保
				resultStr = "Secret answer is not correct(密保答案不正确)!";
			}else{
				//验证通过，执行
				resultStr=accountService.deltaTransfer(loginName,transAccount, value,type);
			}
		}else{
			resultStr = "系统超时，请重新登录!";
		}
		/*HttpSession session = ServletActionContext.getRequest().getSession();
		if(session.getAttribute("loginName")!=null){
			resultStr=accountService.deltaTransfer((String)session.getAttribute("loginName"),transAccount, value,type);
		}else{
			resultStr = "系统超时，请重新登录!";
		}*/
		return "optresult";
	}
	
	public String accountActivate(){
		status=accountService.accountActivate(loginName);
		return "status";
	}
	
	public String findAccountInfoByLoginName(){
		accountDTO = accountService.findAccountInfoByLoginName(loginName);
		
		return "accountDTO";
	}
	public String extract(){
		if(pageUrl==null || pageUrl=="")
			pageUrl="index.jsp";
		HttpSession session = ServletActionContext.getRequest().getSession();
		if(session.getAttribute("loginName")!=null){
			resultStr = accountService.extract((String)session.getAttribute("loginName"), extractMoney);
		}else{
			resultStr = "sessionTimeout";
		}
		return "optresult";
	}
	
	public String addTo(){
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		if(session.getAttribute("loginName")!=null){
			status = accountService.addTo((String)session.getAttribute("loginName"), extractMoney);
		}else{
			status = "sessionTimeout";
		}
		return "status";
	}
	public String gantLookPointAward(){
		accountService.gantLookPointAward();
		return SUCCESS;
	}
	
	public String recharge(){
		accountService.recharge(loginName, value);
		
		return SUCCESS;
		
	}
}
